package cn.schoolwow.quickdao.module.initial.flow.database;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.provider.DatabaseProvider;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.util.List;

public class CheckDataSourceFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        List<DatabaseProvider> databaseProviderList = (List<DatabaseProvider>) flowContext.checkData("databaseProviderList");
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);

        String jdbcUrl = (String) flowContext.checkData("jdbcUrl");
        for (DatabaseProvider databaseProvider : databaseProviderList) {
            if (jdbcUrl.contains("jdbc:" + databaseProvider.name())) {
                quickDAOConfig.databaseContext.databaseProvider = databaseProvider;
                break;
            }
        }
        if (null == quickDAOConfig.databaseContext.databaseProvider) {
            throw new IllegalArgumentException("不支持的数据库类型!jdbcUrl:" + jdbcUrl);
        }

        flowContext.putInstanceData(quickDAOConfig.databaseContext.databaseProvider.getDatabaseType());
    }

    @Override
    public String name() {
        return "检查数据库连接池";
    }
}
